package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.MallHistoryMapper;
import com.hmall.mapper.MallProductMapper;
import com.hmall.model.HistoryVo;
import com.hmall.model.MallProduct;
import com.hmall.model.SysUser;
import com.hmall.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HistoryService {

    private final
    MallHistoryMapper mallHistoryMapper;

    private final
    MallProductMapper mallProductMapper;

    @Autowired
    public HistoryService(MallHistoryMapper mallHistoryMapper, MallProductMapper mallProductMapper) {
        this.mallHistoryMapper = mallHistoryMapper;
        this.mallProductMapper = mallProductMapper;
    }

    public ServerResponse<PageVo<HistoryVo>> viewHistory(Integer userId, int pageNum, int pageSize) {

        int pageStart = pageSize * (pageNum - 1);
        int total = mallHistoryMapper.selectViewCount(userId);
        List<Integer> v = mallHistoryMapper.selectView(userId, pageStart, pageSize);


        //产品的历史
        List<MallProduct> m = mallProductMapper.selectProductList(v);

        List<HistoryVo> historyVoList = new ArrayList<>();

        System.out.println(m.size());
        for(int i = 0; i < v.size(); i++){
            System.out.println("111");
            historyVoList.add(new HistoryVo(v.get(i), m.get(i).getId(), m.get(i).getCategoryId(), m.get(i).getName(),
                  m.get(i).getSubtitle(), m.get(i).getMainImage(), m.get(i).getPrice(), m.get(i).getStock(), m.get(i).getStatus(),
                    m.get(i).getCreateTime(), m.get(i).getUpdateTime(), m.get(i).getProductno(), m.get(i).getShopid(), m.get(i).getUprice(),
                    m.get(i).getCommentNum(), m.get(i).getSpecial(), m.get(i).getSearchHot(), m.get(i).getIsnew(), m.get(i).getSubImages(),
                    m.get(i).getDetail(), m.get(i).getContent()));

        }
        System.out.println(historyVoList);
        PageVo<HistoryVo> pageVo = new PageVo<>(pageNum, pageSize, total, historyVoList);
        System.out.println(pageVo);
        return ServerResponse.createBySuccess(pageVo);
    }

    public ServerResponse deleteHistory(Integer historyId) {

        mallHistoryMapper.deleteHistory(historyId);
        return ServerResponse.createBySuccess();
    }

    public ServerResponse deleteAllHistory(SysUser user) {

        mallHistoryMapper.deleteAllHistory(user.getUserId());
        return ServerResponse.createBySuccess();
    }
}
